<template>
  <div>
    <div v-if="query.dictName === ''">
      <div class="my-code">数据库表</div>
    </div>
    <div v-else>
      <!--工具栏-->
      <div class="head-container">
        <div v-if="crud.props.searchToggle">
          <!-- 搜索 -->
          <el-input v-model="query.label" clearable size="small" placeholder="输入字典标签查询" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
          <rrOperation />
        </div>
      </div>
      <!--表单组件-->
      <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="字典标签" prop="label">
            <el-input v-model="form.label" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="字典值" prop="value">
            <el-input v-model="form.value" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="落标值" prop="mark">
            <el-input v-model="form.mark" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="字段分组" prop="dictGroup">
            <el-select
              v-model="temp"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="请选择分组"
              style="width: 370px;"
            >
              <el-option
                v-for="item in groups"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column :show-overflow-tooltip="true" prop="tableName" label="表名" />
        <el-table-column :show-overflow-tooltip="true" prop="engine" label="数据库引擎" />
        <el-table-column :show-overflow-tooltip="true" prop="coding" label="字符编码集" />
        <el-table-column :show-overflow-tooltip="true" prop="remark" label="备注" />
        <el-table-column prop="createTime" label="创建日期" />
        <el-table-column label="操作" width="160px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" style="margin-right: 2px" type="text">
              <router-link :to="'/sys-tools/generator/preview/' + scope.row.tableName">
                预览
              </router-link>
            </el-button>
            <el-button size="mini" style="margin-left: -1px;margin-right: 2px" type="text" @click="toDownload(scope.row.tableName)">下载</el-button>
            <el-button size="mini" style="margin-left: -1px;margin-right: 2px" type="text">
              <router-link :to="'/sys-tools/generator/config/' + scope.row.tableName">
                配置
              </router-link>
            </el-button>
            <el-button type="text" style="margin-left: -1px" size="mini" @click="toGen(scope.row.tableName)">生成</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDictDetail from '@/api/system/dictDetail'
import { getGroup } from '@/api/system/dictDetail'
import CRUD, { presenter, header, form } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'

const defaultForm = { id: null, label: null, value: null, dictSort: 999, dictGroup: null, mark: null }

export default {
  components: { pagination, rrOperation },
  cruds() {
    return [
      CRUD({ title: '字典详情', url: 'api/generator/tables', query: { dictName: '' }, sort: ['dictSort,asc', 'id,desc'],
        crudMethod: { ...crudDictDetail },
        optShow: {
          add: true,
          edit: true,
          del: true,
          reset: false
        },
        queryOnPresenterCreated: false
      })
    ]
  },
  mixins: [
    presenter(),
    header(),
    form(function() {
      return Object.assign({ dict: { id: this.dictId }}, defaultForm)
    })],
  data() {
    return {
      dictId: null,
      groups: [],
      temp: [],
      rules: {
        label: [
          { required: true, message: '请输入字典标签', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请输入字典值', trigger: 'blur' }
        ],
        dictSort: [
          { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
        ]
      },
      permission: {
        add: ['admin', 'dict:add'],
        edit: ['admin', 'dict:edit'],
        del: ['admin', 'dict:del']
      }
    }
  },
  methods: {
    [CRUD.HOOK.beforeToAdd](crud, form) {
      getGroup().then(res => {
        this.groups = res
      }).catch(() => { })
      this.temp = []
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.beforeToEdit](crud, form) {
      getGroup().then(res => {
        this.groups = res
      }).catch(() => { })
      this.temp = form['dictGroup'].split(',')
    },
    [CRUD.HOOK.beforeSubmit]() {
      this.form.dictGroup = this.temp.join(',')
      this.temp = []
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
